/*
 * Copyright (c) 2009.
 * nick@tmro.net
 */
package net.tmro.loko.web.managedbeans;

import net.tmro.loko.domain.category.Category;
import net.tmro.loko.ejb.dao.CategoryDao;

import javax.ejb.EJB;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;

/**
 * @author: nick
 * Date: Apr 5, 2009
 * Time: 10:01:24 PM
 */
public class CategoryManagedBean {
    final public static String SELECT_ALL_ENTITIES_SQL = "SELECT o FROM Category AS o";

    @EJB(beanInterface = CategoryDao.class, mappedName = "javaEEApplication/CategoryBean/local")
    private CategoryDao categoryJpaDao;

    private Category myEntity;

//    private EntityManagerFactory myEntityManagerFactory;

    private ListDataModel myList;
    private ListDataModel myReferencesEntities; // M-N and M-1 references

    public CategoryManagedBean() {
//        myEntityManagerFactory = Persistence.createEntityManagerFactory("MainLokoPeristenceUnit");
    }

//    private EntityManagerFactory getEntityManagerFactory() {
//        return myEntityManagerFactory;
//    }

    public Category getEntity() {
        return myEntity;
    }

    public void setEntity(Category entity) {
        myEntity = entity;
    }

    // add new Category
    public String create() {
//        EntityManager entityManager = getEntityManagerFactory().createEntityManager();
//        try {
//            entityManager.getTransaction().begin();
//            entityManager.persist(getEntity());
//            entityManager.getTransaction().commit();
//        } catch (Exception ex) {
//            try {
//                entityManager.getTransaction().rollback();
//            } catch (Exception e) {
//            }
//        }
//        entityManager.close();

        final Category category = getEntity();
        System.out.println("Category Id to insert is:" + category.getId());
        categoryJpaDao.persist(category);

        return "categoryList";
    }

    // save edited Category
    public String save() {
//        EntityManager entityManager = getEntityManagerFactory().createEntityManager();
//        try {
//            entityManager.getTransaction().begin();
//            myEntity = entityManager.merge(getEntity());
//            entityManager.getTransaction().commit();
//        } catch (Exception ex) {
//            try {
//                entityManager.getTransaction().rollback();
//            } catch (Exception e) {
//            }
//        }
//        entityManager.close();

        //nick p
        categoryJpaDao.persist(getEntity());

        return "categoryList";
    }

    // delete Category
    public String delete() {
//        EntityManager entityManager = getEntityManagerFactory().createEntityManager();
//        try {
//            entityManager.getTransaction().begin();
//            Category entity = getCurrentEntity();
//            entity = entityManager.merge(entity);
//            entityManager.remove(entity);
//            entityManager.getTransaction().commit();
//        } catch (Exception ex) {
//            try {
//                entityManager.getTransaction().rollback();
//            } catch (Exception e) {
//            }
//        }
//        entityManager.close();

        Category category = getCurrentEntity();
        categoryJpaDao.remove(category);

        return "categoryList";
    }

    public DataModel getReferencedEntities() {
        return myReferencesEntities;
    }

    public void setReferencedEntities(Collection<Category> entities) {
        myReferencesEntities = new ListDataModel(new ArrayList<Category>(entities));
    }

    public String startCreate() {
        myEntity = new Category();
        return "createCategory";
    }

    public String startView() {
        setEntityFromRequestParam();
        return "viewCategory";
    }

    public String startEdit() {
        setEntityFromRequestParam();
        return "editCategory";
    }

    public Category getCurrentEntity() {
        Category entity = getEntityFromRequestParam();

        return entity == null ? myEntity : entity;
    }

    public Category getEntityFromRequestParam() {
        if (myList == null) return null;

//        EntityManager entityManager = getEntityManagerFactory().createEntityManager();
        Category entity = (Category) myList.getRowData();
//        entity = entityManager.merge(entity);
//        entityManager.close();

        return entity;
    }

    public void setEntityFromRequestParam() {
        myEntity = getCurrentEntity();
    }

    public Category findEntity(Long id) {
//        EntityManager entityManager = getEntityManagerFactory().createEntityManager();
//
//        Category entity = entityManager.find(Category.class, id);
//
//        entityManager.close();

        //Nick
        final Category entity = categoryJpaDao.findById(id);

        return entity;
    }

    public DataModel getAllEntities() {
        myList = new ListDataModel(getEntities());

        return myList;
    }

    public SelectItem[] getAllEntitiesAsSelectedItems() {
        List<Category> entities = getEntities();
        SelectItem selectItems[] = new SelectItem[entities.size()];
        int i = 0;
        for (Category entity : entities) {
            selectItems[i++] = new SelectItem(entity);
        }
        return selectItems;
    }

    public List<Category> getEntities() {
//        EntityManager entityManager = getEntityManagerFactory().createEntityManager();
//
//        List<Category> entities = (List<Category>) entityManager.createQuery(SELECT_ALL_ENTITIES_SQL).getResultList();
//
//        entityManager.close();
        final Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 1970);
        final List<Category> entities = categoryJpaDao.findNewCategories(calendar.getTime());

        return entities;
    }
}
